<?php
/**
 *  Create On 2015-6-11
 *  Author jing
 **/


class UploadService extends BaseService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self();
        }

        return self::$self;
    }

   /********************** Model **********************/


    /**
     * @var Involved_TextModel
     */
    private $mTextInfo      = NULL;

    /**
     * @var Involved_AudioModel
     */
    private $mAudioInfo     = NULL;

    /**
     * @var Involved_VideoModel
     */
    private $mVideoInfo     = NULL;

    /**
     * @var Involved_QuestionModel
     */
    private $mQuestionInfo  = NULL;
    
    /**
     * @var Involved_QuestionfavorModel
     */
    private $mQuestionFavorInfo  = NULL;

    /**
     * @var Involved_CommentModel
     */
    private $mCommentInfo   = NULL;

    /**
     * @var Involved_VideoFavorModel
     */
    private $mVideoFavor    = NULL;

    /**
     * @var Involved_CommentFavorModel
     */
    private $mCommentFavor  = NULL;

    /**
     * @var Involved_ItemVideoModel
     */
    private $mItemVideo     = NULL;

    /**
     * @var Involved_NoteModel
     */
    private $mNoteInfo      = NULL;

    /**
     * @var Involved_ImageModel
     */
    private $mImage            = NULL;
    
    /**
     * @var Involved_VideoCommentModel
     */
    private $mVideoComment   = NULL;

  

    public function __construct()
    {
        $this->mTextInfo          = new Involved_TextModel();
        $this->mAudioInfo         = new Involved_AudioModel();
        $this->mVideoInfo         = new Involved_VideoModel();
        $this->mQuestionInfo      = new Involved_QuestionModel();
        $this->mQuestionFavorInfo = new Involved_QuestionFavorModel();
        $this->mCommentInfo       = new Involved_CommentModel();
        $this->mVideoFavor        = new Involved_VideoFavorModel();
        $this->mCommentFavor      = new Involved_CommentFavorModel();
        $this->mItemVideo         = new Involved_ItemVideoModel();
        $this->mNoteInfo          = new Involved_NoteModel();
        $this->mImage             = new Involved_ImageModel();
        $this->mVideoComment      = new Involved_VideoCommentModel();
    }
    
    public function upload_image($file)
    {
    	#文件上传
    	$clientName = $file->getClientOriginalName();
    	$extension = $file->getClientOriginalExtension() ? $file->getClientOriginalExtension() : "jpg";
    	$newName = md5(date('ymdhis').$clientName).".".$extension;
    	$file -> move(public_path().'/resource/avator',$newName);
    	
    	if($file->getError()){
    		throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    	}
    	$qiniuConf = Config::get("qiniu");
    	QiniuUtil::instance()->setConfig($qiniuConf['accessKey'], $qiniuConf['secretKey'],$qiniuConf['bucket_images']);
    	$results = QiniuUtil::instance()->upload_files($newName, public_path().'/resource/avator/'.$newName);
    	if ($results){    		
    		#记录文件
    		$aData = array(
    		'name' => $newName,
    		'create_time' => date('y-m-d H:i:s', time())
    		);
    		return $this->mImage->insert($aData);
    	}else  throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    }
    public function upload_audio($file)
    {
    	#文件上传
    	$clientName = $file->getClientOriginalName();
    	$extension  = $file->getClientOriginalExtension()?$file->getClientOriginalExtension():"aac";
    	$newName    = md5(date('ymdhis').$clientName).".".$extension;
    	$file->move(public_path().'/resource/attach',$newName);
    	
    	if($file->getError()){
    		throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    	}
    	//上传七牛
    	$qiniuConf = Config::get("qiniu");
    	QiniuUtil::instance()->setConfig($qiniuConf['accessKey'], $qiniuConf['secretKey'],$qiniuConf['bucket_audio']);
    	$result = QiniuUtil::instance()->upload_audio($newName, public_path().'/resource/attach/'.$newName);
    	if ($result){
    		#记录文件
    		$aData = array(
	    		'name' => $newName,
	    		'time' => date('y-m-d H:i:s', time())
    		);
    		return $this->mAudioInfo->insert($aData);    		
    	}else throw new Exception('附件上传失败！', ErrorCodeEnum::STATUS_ERROR);
    }
    	
}